import { Application } from 'egg';
import {
  indexRule,
  createRule,
  updateRule,
  destroyRule,
 } from '../validate/goods/type';

/**
 * 商品类型管理
 */
export default (app: Application) => {
  const { auth } = app.middleware;
  const {  jwt } = app;

  return {
    'goods.type.all': {
      method: 'GET',
      path: '/api/goods/type/all',
      options: {
        tags: ['type/list/all'],
        description: '商品类型列表',
        handler: app.controller.goods.type.all,
        middlewares: [jwt, auth('goods.type.all')],
        validate: indexRule,
      },
    },
    'goods.type.list': {
      method: 'GET',
      path: '/api/goods/type',
      options: {
        tags: ['type/list'],
        description: '商品类型列表',
        handler: app.controller.goods.type.list,
        middlewares: [jwt, auth('goods.type.list')],
        validate: indexRule,
      },
    },
    'goods.type.create': {
      method: 'POST',
      path: '/api/goods/type',
      options: {
        tags: ['type/create'],
        description: '创建商品类型',
        handler: app.controller.goods.type.create,
        middlewares: [jwt, auth('goods.type.create')],
        validate: createRule,
      },
    },
    'goods.type.update': {
      method: 'PUT',
      path: '/api/goods/type/:id',
      options: {
        tags: ['type/update'],
        description: '更新商品类型',
        handler: app.controller.goods.type.update,
        middlewares: [jwt, auth('goods.type.update')],
        validate: updateRule,
      },
    },
    'goods.type.destroy': {
      method: 'DELETE',
      path: '/api/goods/type/:id',
      options: {
        tags: ['type/delete'],
        description: '删除商品类型',
        handler: app.controller.goods.type.destroy,
        middlewares: [jwt, auth('goods.type.destroy')],
        validate: destroyRule,
      },
    }
  };
};
